﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using Phone.Framework;
using System.Data.Linq;
using Phone.Framework.Data;
using System.Collections.Generic;
using System.Text.RegularExpressions;



namespace CetWin.Phone.Entity
{

    public partial class CetWinDbDataContext : DbDataContext
    {
        private static CetWinDbDataContext db;

        public static CetWinDbDataContext GetDataContext()
        {
            if (db == null)
                db = DataBaseHelper.CreateDataBase<CetWinDbDataContext>("cet2");
            return db;
        }

        public static CetWinDbDataContext GetNewDataContext()
        {
            return DataBaseHelper.CreateDataBase<CetWinDbDataContext>("cet2");
        }

        public CetWinDbDataContext(string connectionString)
            : base(connectionString)
        { }

        public Table<Favorite> Favorites;

        public Table<Word> Words;

        public Table<History> Histories;
        
        public Table<Category> Categories;

        public Table<CategoryWord> CategoryWords;

        public Table<WordExtesion> WordExtesions;

        public Table<People> Peoples;

        public Table<Name> Names;
    }

    [Table]
    public class People : PropertyChangedBase
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }


        [Column]
        internal int _nameId;

        // Entity reference, to identify the ToDoCategory "storage" table
        private EntityRef<Name> _name;

        // Association, to describe the relationship between this key and that "storage" table

        [Association(Storage = "_name", ThisKey = "_nameId", OtherKey = "Id")]
        public Name Name
        {
            get { return _name.Entity; }
            set
            {
                _name.Entity = value;
                if (value != null)
                {
                    _nameId = value.Id;
                }
                NotifyPropertyChanged("Name");
            }
        }

        private int _age;
        [Column]
        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
                NotifyPropertyChanged("Age");
            }
        }
    }

    [Table]
    public class Name : PropertyChangedBase
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _firstName;
        [Column]
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }

        private string _lastName;
        [Column]
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                NotifyPropertyChanged("LastName");
            }
        }

        //[Column]
        //internal int _peopleId;

        //// Entity reference, to identify the ToDoCategory "storage" table
        //private EntityRef<People> _people;

        //// Association, to describe the relationship between this key and that "storage" table

        //[Association(Storage = "_people", ThisKey = "_peopleId", OtherKey = "Id")]
        //public People People
        //{
        //    get { return _people.Entity; }
        //    set
        //    {
        //        _people.Entity = value;
        //        if (value != null)
        //        {
        //            _peopleId = value.Id;
        //        }
        //        NotifyPropertyChanged("Name");
        //    }
        //}



        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;
    }

    [Table]
    public class Favorite : PropertyChangedBase
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        // Define category name: private field, public property, and database column.
        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public string HistoryTime
        {
            get
            {
                var now=DateTime.Now;
                var nowTime=new DateTime(now.Year,now.Month,now.Day);
                var currentTime=new DateTime(Time.Year,Time.Month,Time.Day);
                if (nowTime == currentTime)
                    return "Today";
                if (currentTime.AddDays(1) == nowTime)
                    return "Yesterday";

                return "Earlier";
            }
        }

        private DateTime _time;

        [Column]
        public DateTime Time
        {
            get { return _time; }
            set
            {
                _time = value;
                NotifyPropertyChanged("Time");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;
    }
    public interface IWord
    {
        string Name { get; set; }
    }

    [Table]
    public class History : PropertyChangedBase
    {

        private int _id;
        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private DateTime _time;
        [Column]
        public DateTime Time
        {
            get { return _time; }
            set
            {
                _time = value;
                NotifyPropertyChanged("Time");
            }
        }

        private int _count=0;
        [Column]
        public int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                //NotifyPropertyChanged("Count");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;
    }

    [Table]
    public class WordExtesion : PropertyChangedBase
    {
        private int _id;
        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private string _comment;
        [Column]
        public string Comment
        {
            get { return _comment; }
            set
            {
                _comment = value;
                NotifyPropertyChanged("Comment");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;
    }
  

    [Table]
    public class Word : PropertyChangedBase, IWord
    {

        public Word()
        {
            //WordExtesion = new WordExtesion();
           // Status = new Dictionary<string, object>();
        }

        // Define ID: private field, public property, and database column.
        private int _id;
        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _explain;
        [Column]
        public string Explain
        {
            get { return _explain; }
            set
            {
                _explain = value;
                NotifyPropertyChanged("Explain");
            }
        }


        [Column]
        internal int _wordExtesionId;

        private EntityRef<WordExtesion> _wordExtesion;

        [Association(Storage = "_wordExtesion", ThisKey = "_wordExtesionId", OtherKey = "Id")]
        public WordExtesion WordExtesion
        {
            get
            {
                return _wordExtesion.Entity;
            }
            set
            {
                _wordExtesion.Entity = value;
                if (value != null)
                {
                    _wordExtesionId = value.Id;
                }
                NotifyPropertyChanged("WordExtesion");
            }
        }

        private string _category;
        [Column]
        public string Category
        {
            get { return _category; }
            set
            {
                _category = value;
                NotifyPropertyChanged("Category");
            }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        //public List<WordType> Type
        //{
        //    get
        //    {
        //        List<WordType> wordTypes = new List<WordType>();

        //        if (Regex.IsMatch(Explain.Substring(0, 1), "[A-Za-z]"))
        //        {
        //            //find the n.\adj.\adv.\v.\vt.\vi., etc.
        //            //int typeSplitPos = Explain.IndexOf(".", 0);
        //            //return Explain.Substring(0, typeSplitPos);
        //        }
        //        else
        //        {
        //            wordTypes.Add(WordType.None);
        //        }
        //        return wordTypes;
        //    }
        //}

        private string _pron;
        [Column]
        public string Pron
        {
            get { return _pron; }
            set
            {
                _pron = value;
                NotifyPropertyChanged("Pron");
            }
        }

        private string _audio;
        [Column]
        public string Audio
        {
            get { return _audio; }
            set
            {
                _audio = value;
                NotifyPropertyChanged("Audio");
            }
        }

        private int _cetLevel;
        public int CetLevel
        {
            get { return _cetLevel; }
            set
            {
                _cetLevel = value;
                NotifyPropertyChanged("CetLevel");
            }
        }

        private bool _isCet4Level;
        public bool IsCet4Level
        {
            get { return _isCet4Level; }
            set
            {
                _isCet4Level = value;
                NotifyPropertyChanged("IsCet4Level");
            }
        }

        private bool _isCet6Level;
        public bool IsCet6Level
        {
            get { return _isCet6Level; }
            set
            {
                _isCet6Level = value;
                NotifyPropertyChanged("IsCet6Level");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        public Dictionary<string, object> Status { get; set; }
        

    }

    [Table]
    public class CategoryWord : PropertyChangedBase
    {
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private int _catId;

        [Column]
        public int CatId
        {
            get { return _catId; }
            set
            {
                _catId = value;
                NotifyPropertyChanged("CatId");
            }
        }

        private string _wordName;

        [Column]
        public string WordName
        {
            get { return _wordName; }
            set
            {
                _wordName = value;
                NotifyPropertyChanged("WordName");
            }
        }
        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;
    }

    [Table]
    public class Category : PropertyChangedBase
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _name;

        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private IEnumerable<CategoryWord> _words;
      
        public IEnumerable<CategoryWord> Words
        {
            get { return _words; }
            set
            {
                _words = value;
                NotifyPropertyChanged("Words");
            }
        }

        private string _type;

        [Column]
        public string Type
        {
            get { return _type; }
            set
            {
                _type = value;
                NotifyPropertyChanged("Type");
            }
        }
        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        public static string NormalType = "Normal";
        public static string DateType = "Date";
    }
}
